package cn.org.july.weixin.service.menu;

import cn.org.july.weixin.common.bean.menu.Button;
import cn.org.july.weixin.common.constant.ButtonType;
import cn.org.july.weixin.common.constant.WxConstant;
import cn.org.july.weixin.common.utils.HttpClientUtils;
import cn.org.july.weixin.service.accesstoken.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 创建菜单处理逻辑类
 * Created with IntelliJ IDEA.
 * User:  wanghongjie
 * Date:  2020/9/1 - 06:47
 * <p>
 * Description:
 */
@Service
public class MenuService {

    @Autowired
    private AccessTokenService accessTokenService;

    /**
     * 创建菜单
     *
     * @return
     */
    public String createMenu() throws IOException {

        Button one_1 = Button.ofOneMenu(Button.HEAD, ButtonType.VIEW, "关于我们", "http://hryreadingroom.cn/about/");
        Button one_2 = Button.ofOneMenu(one_1, ButtonType.VIEW, "活动", "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzUzNzk2OTM2NA==#wechat_redirect");
        Button one_3 = Button.ofOneMenu(one_2, ButtonType.CLICK, "会员中心", "会员中心");
        Button of = Button.of(one_3, 2, ButtonType.VIEW, "我的会员", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx68108a81fcd84c10&redirect_uri=http://hryreadingroom.cn/mobile/v1/user/oauthLogin/&response_type=code&scope=snsapi_userinfo#wechat_redirect");
        Button of1 = Button.of(of, 2, ButtonType.VIEW, "周边商户", "http://hryreadingroom.cn/mobile/v1/artist");
        Button of2 = Button.of(of1, 2, ButtonType.VIEW, "积分商城", "http://hryreadingroom.cn/mobile/v1/goods");
        Button of3 = Button.of(of2, 2, ButtonType.VIEW, "切换用户", "http://hryreadingroom.cn/mobile/v1/user/logout");
        String url = WxConstant.CREATE_MENU_URL.replace("ACCESS_TOKEN", accessTokenService.getAccessToken());
        System.out.println(of3.toJson());
        return HttpClientUtils.post(url, of3.toJson());
    }

    /**
     * 删除菜单
     *
     * @return 删除菜单
     */
    public String deleteMenu() throws IOException {
        String url = WxConstant.DELETE_MENU_URL.replace("ACCESS_TOKEN", accessTokenService.getAccessToken());
        return HttpClientUtils.get(url);
    }
}